<?php
/**
 * 品牌模型
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
class BrandModel extends UE_Model {
    public $table  = 'ue_brand';
	public $key    = 'brand_id';
	
	public function add($brand) {
		if (empty($brand['brand_name'])) {
			$this->setErr('品牌名称不能为空');
		    return false;
		}
		
	    UE_Loader::loadModel('UploaderModel', 'system');
		// logo
		$brandLogoInfo = array();
		if(!empty($_FILES['brand_logo']['tmp_name'])) {
		    $uploader = new UploaderModel();
		    $uploader->setField('brand_logo');
		    $brandLogoInfo = $uploader->add(array('upload_app'=>'product', 'upload_type' => 'brand'));
		    $brandLogoInfo && $brand['brand_logo_id'] = $brandLogoInfo['upload_id'];
		     //print_r($brandLogoInfo);
		}
		
		$brandId = parent::add($brand);
		
		// 默认排序跟序号一样
		if(empty($brand['brand_order'])) {
			$sql = "UPDATE {$this->table} SET brand_order=brand_id WHERE brand_id=$brandId";
			UE_DB::factory()->query($sql);
		}
				
		// 关联栏目
		if(!empty($brand['cat_id'])) {
			foreach($brand['cat_id'] as $catId) {
				$sql = "INSERT INTO ue_brand_r_cat (brand_id, cat_id) VALUE ('{$brandId}', $catId)";
				UE_DB::factory()->query($sql);
			}
		}
		
		return $brandId;
	}
	
	public function update($brand) {		
		if (empty($brand['brand_name'])) {
			$this->setErr('品牌名称不能为空');
		    return false;
		}
		
	    UE_Loader::loadModel('UploaderModel', 'system');
		// logo
		$brandLogoInfo = array();
		if(!empty($_FILES['brand_logo']['tmp_name'])) {
			// TODO 删除旧的logo
		    $uploader = new UploaderModel();
		    $uploader->setField('brand_logo');
		    $brandLogoInfo = $uploader->add(array('upload_app'=>'product', 'upload_type' => 'brand'));
		    $brandLogoInfo && $brand['brand_logo_id'] = $brandLogoInfo['upload_id'];
		     //print_r($brandLogoInfo);
		}
		
		if(!parent::update($brand)) {
			return false;
		}
		
		// 关联栏目
		$sql = "DELETE FROM ue_brand_r_cat WHERE brand_id= '{$this->id}'";
		if (!UE_DB::factory()->query($sql)) {
			return false;
		}
		
		if(!empty($brand['cat_id'])) {
			foreach($brand['cat_id'] as $catId) {
				$sql = "INSERT INTO ue_brand_r_cat (brand_id, cat_id) VALUE ('{$this->id}', $catId)";
				if(!UE_DB::factory()->query($sql)) {
					return false;
				}
			}
		}
		
		return true;
	}
	
	/**
	 * 禁用品牌
	 *
	 * @return bool
	 */
	public function disable() {
		if (!$this->check()) {
			return false;
		}
		$sql = "UPDATE ue_brand SET brand_enabled=0 WHERE brand_id='{$this->id}' LIMIT 1";
		return UE_DB::factory()->query($sql);
	}
	
	/**
	 * 启用品牌
	 *
	 * @return bool
	 */
	public function enable() {
		if (!$this->check()) {
			return false;
		}
		$sql = "UPDATE ue_brand SET brand_enabled=1 WHERE brand_id='{$this->id}' LIMIT 1";
		return UE_DB::factory()->query($sql);
	}
	
	/**
	 * 获取品牌
	 *
	 * @param int $type 0:只返回垃圾分类， 1： 不返回垃圾分类，2:垃圾和非垃圾都返回
	 */
	public static function getBrands($type = 1) {
		if ($type == 2) {
			$filter = 'true';
		} else if ($type == 1) {
			$filter = 'brand_is_trash != 1';
		} else {
			$filter = 'brand_is_trash = 1';
		}
		
		$cache = UE_Cache::factory();
		if ($brands = $cache->get('brands-'.$type)) {
			return $brands;
		}
		
		$sql = "SELECT * FROM ue_brand WHERE $filter ORDER BY brand_order ASC";
		$brands = UE_DB::factory()->getAll($sql);
		$cache->set('brands-'.$type, $brands);
		
		return $brands;
	}
}